package cn.gok.plusdemo.controller;


import cn.gok.plusdemo.entity.Student;
import cn.gok.plusdemo.service.StudentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 林
 * @since 2022-07-22
 */
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    //查询所有学生信息
    @RequestMapping("/findAll")
    public List<Student> findAllStudent() {
        return studentService.list(null);
    }

    //通过ID查询学生信息
    @RequestMapping("/findById")
    public Student findStudentById(@RequestParam Long id) {
        return studentService.getById(id);
    }

    //模糊查询学生信息
    @RequestMapping("/mohu")
    public List<Student> mohu(@RequestParam String sname){
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like("sname",sname);
        return studentService.list(queryWrapper);
    }

    //分页查询学生信息
    @RequestMapping("/page")
    public IPage<Student> page(){
        Page<Student> page = new Page<>(1,5);
        return studentService.page(page,null);
    }

    //添加学生信息
    @RequestMapping("/add")
    public boolean addStudent(@RequestBody Student student) {
        return studentService.save(student);
    }

    //修改学生信息
    @RequestMapping("/update")
    public boolean updateStudentById(@RequestBody Student student) {
        return studentService.updateById(student);
    }

    //根据ID删除学生
    @RequestMapping("/deleById")
    public boolean deleStudentById(@RequestParam Long id) {
        return studentService.removeById(id);
    }

    //批量删除学生
    @RequestMapping("/deleByIds")
    public boolean removeStudentByIds(@RequestBody List<Integer> ids) {
        return studentService.removeByIds(ids);
    }
}

